package csu.web.jpetstore.web.servlet;

import com.mysql.cj.Session;
import csu.web.jpetstore.domain.Account;
import csu.web.jpetstore.domain.Cart;
import csu.web.jpetstore.domain.Order;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class NewOrderViewServlet extends HttpServlet {

    private static final String NEW_ORDER_VIEW = "/WEB-INF/jsp/order/newOrderView.jsp";
    private static final String CART_VIEW = "/WEB-INF/jsp/cart/cart.jsp";

    private String newOrdMsg;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Account loginAccount = (Account) session.getAttribute("loginAccount");
        Cart cart = (Cart) session.getAttribute("cart");

        if(loginAccount == null){
            resp.sendRedirect("signOnView");//未登录，转到登录界面
        }else if(cart == null){
            this.newOrdMsg = "An order could not be created because a cart could not be found.";
            session.setAttribute("newOrdMsg", newOrdMsg);
            req.getRequestDispatcher(CART_VIEW).forward(req,resp);
        }else{
            Order order = new Order();
            order.initOrder(loginAccount, cart);
            session.setAttribute("order",order);
            req.getRequestDispatcher(NEW_ORDER_VIEW).forward(req,resp);
        }
    }
}